<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;

class RecruitController extends Controller
{
    public function getRecruitList(){

        try{
            $results = DB::table('recruit_info')->paginate(5);

            return view('Admin.recruitlist',['results'=> $results]);

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function addRecruit(){

        try{
            $data = Request::only('recruit_info_department','recruit_info_position','editorValue','recruit_info_introduce','recruit_info_contactman','recruit_info_contactemail','recruit_info_contacttel');

           // dd($data);
            $validator = Validator::make($data, [
                'recruit_info_department' => 'required',
                'recruit_info_position' => 'required',
                'recruit_info_introduce' => 'required',
                'recruit_info_contactman' => 'required',
                'recruit_info_contactemail' => 'required|email',
                'recruit_info_contacttel' => 'required|digits:11',
            ]);


            if ($validator->fails()) {
                return $validator->messages();
            }
            $data['recruit_info_require'] =  $data['editorValue'];
            unset($data['editorValue']);
             //dd($data);

            $result = DB::table('recruit_info')->insert($data);
            if($result){

                return redirect('/admin/getRecruitList');
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function updateRecruit(){

        try{
            $data = Request::only('recruit_info_id','editorValue','recruit_info_department','recruit_info_position','recruit_info_introduce','recruit_info_contactman','recruit_info_contactemail','recruit_info_contacttel');

            // dd($data);
            $validator = Validator::make($data, [
                'recruit_info_id' => 'required',
                'recruit_info_department' => 'required',
                'recruit_info_position' => 'required',
                'recruit_info_introduce' => 'required',
                'recruit_info_contactman' => 'required',
                'recruit_info_contactemail' => 'required|email',
                'recruit_info_contacttel' => 'required|digits:11',
            ]);


            if ($validator->fails()) {
                return $validator->messages();
            }
            $data['recruit_info_require'] =  $data['editorValue'];
            unset($data['editorValue']);
            //dd($data);
            $result = DB::table('recruit_info')->where('recruit_info_id','=',$data['recruit_info_id'])->update($data);
            if($result){

                return redirect('/admin/getRecruitList');
            }
        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function deleteRecruit(){

        try{
            $data = Request::only('recruit_info_id');
            $results = DB::table('recruit_info')->where('recruit_info_id','=',$data['recruit_info_id'])->delete();
            if($results){

                return redirect('/admin/getRecruitList');
            }

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }

    }

}
